//
//  OpinionViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/1.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "OpinionViewController.h"

@interface OpinionViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet SZTextView *textView;

@end

@implementation OpinionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *title, *rightTitle, *placeholder;
    switch (self.type) {
        case OpinionTypeFeedBack:
            title = @"意见反馈";
            placeholder = @"请留下您宝贵的意见和建议";
            rightTitle = @"提交";
            break;
        case OpinionTypeRefund:
            title = @"驳回原因";
            placeholder = @"请填写您驳回退款的原因";
            rightTitle = @"确认";
            break;
//        case OpinionTypeReport:
//            title = @"举报内容";
//            placeholder = @"请填写您举报的内容";
//            rightTitle = @"确认";
        default:
            break;
    }
    self.title = title;
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:rightTitle style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor ThemeColor] forKey:NSForegroundColorAttributeName];
    [right setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    [self hiddenShadowImage:NO];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.textView becomeFirstResponder];
    
}
- (void)rightBarButtonAction:(id)sender {
    NSLog(@"提交");
    if (!TextIsNotNil(_textView.text)) {
        NSString *alertStr;
        switch (self.type) {
            case OpinionTypeFeedBack:
                alertStr = @"请输入您的反馈意见";
                break;
            case OpinionTypeRefund:
                alertStr = @"请输入您的驳回原因";
                break;
//            case OpinionTypeReport:
//                alertStr = @"请输入您举报的内容";
//                break;
            default:
                break;
        }
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:alertStr animated:YES];
        return;
    }
    
    switch (self.type) {
        case OpinionTypeFeedBack:
        {
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            __weak typeof(self) weakSelf = self;
            [MYAFNetworking POST:URL_Save_Feedback parameters:@{@"fcontent":_textView.text} success:^(NSURLSessionDataTask *task, id responseObject) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                NSDictionary *dics = responseObject;
                if ([[dics objectForKey:@"code"] isEqualToString:@"200"]) {
                    [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"提交成功" animated:YES];
                    [weakSelf disMissSelf];
                } else {
                    [MBProgressHUD showError:weakSelf.view title:dics[@"message"] code:[dics objectForKey:@"code"] animated:YES];
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [MBProgressHUD showError:weakSelf.view title:@"" code:@"" animated:YES];
            }];
        } break;
        case OpinionTypeRefund:
        {
            if (self.block) {
                self.block(_textView.text);
            }
            [self disMissSelf];
        } break;
//        case OpinionTypeReport:
//        {
//            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//            __weak typeof(self) weakSelf = self;
//            [MYAFNetworking POST:URL_Save_Report parameters:@{@"content":_textView.text,@"osgid":self.osgid} success:^(NSURLSessionDataTask *task, id responseObject) {
//                [MBProgressHUD hideHUDForView:self.view animated:YES];
//                NSDictionary *dics = responseObject;
//                if ([[dics objectForKey:@"code"] isEqualToString:@"200"]) {
//                    [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"提交成功" animated:YES];
//                    [weakSelf disMissSelf];
//                } else {
//                    [MBProgressHUD showError:weakSelf.view title:dics[@"message"] code:[dics objectForKey:@"code"] animated:YES];
//                }
//            } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
//                [MBProgressHUD hideHUDForView:self.view animated:YES];
//                [MBProgressHUD showError:weakSelf.view title:@"" code:@"" animated:YES];
//            }];
//        } break;
        default:
            break;
    }
}
#pragma mark - UITextViewDelegate
#define LimitMaxWord 255
- (void)textViewDidChange:(UITextView *)textView
{
    NSString *toBeString = textView.text;
    NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage; // 键盘输入模式
    if ([lang hasPrefix:@"zh-Han"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > LimitMaxWord) {
                textView.text = [toBeString substringToIndex:LimitMaxWord];
            }
            //[self.inputTextLabel setText:[NSString stringWithFormat:@"%ld/%ld字",textView.text.length,LimitMaxWord]];
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > LimitMaxWord) {
            textView.text = [toBeString substringToIndex:LimitMaxWord];
        }
        //[self.inputTextLabel setText:[NSString stringWithFormat:@"%ld/%ld字",textView.text.length,LimitMaxWord]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
